<extend name="Public:template" />
<block name="page_header">
</block>
<block name="page_body">
<!-- 配送地址管理页面  -->
<header class="headerSub clearfix">
    <h2>配送地址管理</h2>
    <a href="{:U('Product/index')}" class="leftBtn" >继续购物</a>
    <a href="{:U('Cust/index')}" class="rightBtn yellowNorBg" >我的小屋</a>
</header>
<div class="content">
	<!-- 地址列表 -->
	<div class="listFormBox">
		<volist name="list" id="val">
		<dl id="address{$val.id}" class="itemFormBox">
			<dt><span class="fl">地址{$i}:{$val.address}</span><span class="fr" style="width:20px;">↓</span></dt>  
			<dd class="<if condition='$val.is_default eq 0'>none</if>">
				<span id="msg_info_upd{$val.id}" class="errHolder" style="display:none;"></span>
				<form action="{:U('CustAddress/doUpd')}" name="form_upd{$val.id}" id="form_upd{$val.id}">
					<input type="hidden" name="id" value="{$val.id}">
					<ul class="inputBox">
						<li><input  name="name" type="text" class="inputIconUser" placeholder="收货人" value="{$val.name}" /> </li>
						<li><input  name="mobile" type="number" class="inputIconUser" placeholder="联系方式（手机或电话）" value="{$val.mobile}" /></li>
						<li><input  name="address" type="text" class="inputIconPwd" placeholder="配送地址" value="{$val.address}" /></li>
					</ul>
					<ul class="linkBtnBox">
						<li class="linkBtnBoxL"><a href="#" class="linkBtn yellowNorBg" onClick="doUpd('{$val.id}')">修改</a></li>
						<li class="linkBtnBoxR"><a href="#" class="linkBtn greyNorBg" onClick="doDel('{$val.id}')">删除</a></li>
					</ul>
				</form>
			</dd>
		</dl>
		</volist>
		<dl id="address" class="itemFormBox">
			<dt>添加新地址</dt>  
			<dd class="<notempty name='list'>none</notempty>">
				<span id="msg_info_add" class="errHolder" style="display:none;"></span>
				<form action="{:U('CustAddress/doAdd')}" name="form_add" id="form_add">
					<ul class="inputBox">
						<li><input  name="name" type="text" class="inputIconUser" placeholder="收货人" value="{$cust.name}"/> </li>
						<li><input  name="mobile" type="number" class="inputIconUser" placeholder="联系方式（手机或电话）" value="{$cust.mobile}" /></li>
						<li><input  name="address" type="text" class="inputIconPwd" placeholder="配送地址" /></li>
					</ul>
					<ul class="linkBtnBox">
						<li class="linkBtnBoxFull"><a href="#" class="linkBtn greenNorBg" onClick="doAdd()">保存</a></li>
					</ul>
				</form>
			</dd>
		</dl>
	</div>
</div>
</block>
<block name="script_footer">
<script type="text/javascript">
$(document).ready(function(){
	$(".itemFormBox dt").on('click',function(){
		$(".itemFormBox dt").removeClass("greenGraBg yellowGraBg");
		$(".itemFormBox dd").hide();
		if($(this).parent().attr('id')=='address'){
			$(this).addClass("greenGraBg");
		}else{
			$(this).addClass("yellowGraBg");
		}
		$(this).siblings("dd").show();
		
	})
})
function doUpd(id){
	var msgId = "#msg_info_upd"+id;
	var formId = "#form_upd"+id;
	$(msgId).hide();
	$.ajax({
        'url': $(formId).attr("action"),
        'type':"post",
        'dataType': "json",
        'data': $(formId).serialize(),
        'success': function (data) {
        	var error = data['error'];
        	var message = data['message'];
        	if(error=='0'){
        		$("#address"+id+" dt").html("修改成功！");
        	}else{
        		$(msgId).html(message);
        		$(msgId).show();
        	}
        	
        }
    });
	return false;
}

function doAdd(){
	var msgId = "#msg_info_add";
	var formId = "#form_add";
	$(msgId).hide();
	$.ajax({
        'url': $(formId).attr("action"),
        'type':"post",
        'dataType': "json",
        'data': $(formId).serialize(),
        'success': function (data) {
        	var error = data['error'];
        	var message = data['message'];
        	if(error==0){
        		$(msgId).html(message);
            	$(msgId).show();
        		document.location.href="/CustAddress/index.html";
        	}else{
        		$(msgId).html(message);
            	$(msgId).show();
        	}
        	
        }
    });
	return false;
}

function doDel(id){
	if(!confirm("确定删除该配送信息？")){
		return false;
	}
	var msgId = "#msg_info_upd"+id;
	var formId = "#form_upd"+id;
	$.ajax({
		'url': "{:U('CustAddress/doDel')}",
        'type':"post",
        'dataType': "json",
        'data': $(formId).serialize(),
        'success': function (data) {
        	var error = data['error'];
        	var message = data['message'];
        	if(error==0){
        		document.location.href="/CustAddress/index.html";
        	}else{
        		$(msgId).html(message);
            	$(msgId).show();
        	}
        	
        }
    });
	return false;
}
</script>
</block>